package cn.stylefeng.guns.modular.business.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.neo4j.entity.Figure;
import cn.stylefeng.guns.modular.business.service.Neo4jService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@ApiResource(name = "图谱")
public class Neo4jController {
    static String EVENT = "event";
    static String FIGURE = "figure";
//    Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private Neo4jService neo4jService;

    @GetResource(path = "/neo4j/findLikeTitle")
    public ResponseData findLikeTitle(String title) {
        try {
            List<Object> like = neo4jService.findLike(title);
            return new SuccessResponseData(like);
        } catch (Exception e) {
//            log.error("findLikeTitle出错！", e);
            return new ErrorResponseData("500","findLikeTitle出错！");
        }

    }
    @GetResource(path = "/neo4j/findByTitle")
    public ResponseData findByTitle(String title, String form , String indexId) {
        List<Object> list = new LinkedList<>();
        try {
            List<Object> list1 = neo4jService.findNodeAllByTitle(title);
            list1.addAll(neo4jService.findDynastyAllByTitle(title));
            list1.addAll(neo4jService.findEventAllByTitle(title));
            list1.addAll(neo4jService.findOfficeAllByTitle(title));
            list1.addAll(neo4jService.findFigureAllByTitle(title));
            list1.addAll(neo4jService.findTownAllByTitle(title));
            list1.addAll(neo4jService.findRiverwayAllByTitle(title));
            list1.addAll(neo4jService.findRuinAllByTitle(title));

            JSONArray jsonArray = JSONUtil.parseArray(list1);
            JSONArray jsonArray1 = JSONUtil.createArray();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = JSONUtil.parseObj(jsonArray.get(i));
                String forms = jsonObject.get("form").toString();
                if (form.equals(forms)) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    if (!"".equals(indexId) && !"0".equals(indexId) && !indexId.isEmpty()) {
                        if (indexId.equals(jsonObject1.get("indexId"))) {
                            jsonArray1.add(jsonArray.remove(i));
                        }
                    } else {
                        jsonArray1.add(jsonArray.remove(i));
                    }
                }
            }
            return new SuccessResponseData(jsonArray1);

        } catch (Exception e) {
//            log.error("findByTitle出错！", e);
            return new ErrorResponseData("500", "findByTitle出错！");
        }
    }


    @GetResource(path = "/neo4j/mapByyear")
    public ResponseData mapByyear(String startyear, String endyear, String type, String value) {
        try {
            List<Map<String, Object>> list = new ArrayList<>();
            List objectList = new ArrayList<>();
            Long start = Long.parseLong(startyear);
            Long end = Long.parseLong(endyear);
            if (EVENT.equals(type)) {
                list = neo4jService.findEvenByAdyear(start, end, value);
            } else if (FIGURE.equals(type)) {
                list = neo4jService.findFigureByYear(start, end, value);
                ;
            } else if ("river".equals(type)) {
                if ("".equals(value)) {
                    objectList = neo4jService.findRiverwayJwdByTitle("All");
                } else {
                    objectList = neo4jService.findRiverwayJwdByTitle(value);
                }
            } else if ("town".equals(type)) {
                list = neo4jService.findTownJwdByTitle(value);
            } else if ("build".equals(type)) {
                list = neo4jService.getRuinAll(value);
            }
            if (list.isEmpty()) {
                return new SuccessResponseData(objectList);
            } else {
                return new SuccessResponseData(list);
            }

        } catch (Exception e) {
//            log.error("mapByyear出错！", e);
            return new ErrorResponseData("500", "mapByyear出错！");
        }
    }

    @GetResource(path = "/neo4j/getFigure")
    public String getFigure(String title) {
        List<Figure> lisl = neo4jService.getFigure(title, 25);
        return JSONUtil.toJsonStr(lisl);

    }

}
